package cn.wolfcode.luowowo.mgrsite.listener;



import cn.wolfcode.luowowo.article.domain.StrategyDetail;
import cn.wolfcode.luowowo.article.service.IStrategyDetailService;
import cn.wolfcode.luowowo.cache.domain.StrategyStatisVO;
import cn.wolfcode.luowowo.cache.service.IStrategyStatisRedisService;
import cn.wolfcode.luowowo.cache.util.RedisKeys;
import cn.wolfcode.luowowo.common.util.BeanUtil;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;
import java.util.List;

/**
 * redis数据初始化监听器
 *
 * ContextRefreshedEvent：容器重新启动或加载触发的事件
 */

//实现这个接口成为监听器，<>里面是操作的业务类型，初始化数据

//@Component  //交给spring容器管理
public class RedisDataInitListener  implements ApplicationListener<ContextRefreshedEvent> {

    @Reference
    private IStrategyDetailService strategyDetailService;

    @Reference
    private IStrategyStatisRedisService strategyStatisRedisService;
    //容器启动时执行这个方法
    @Override
    public void onApplicationEvent(ContextRefreshedEvent cont) {
//        System.out.println("------------------------------");
//        //获取攻略数据
//        //获取设置数据到Vo对象
//        //同步到redis
//        List<StrategyDetail> list = strategyDetailService.list();
//        StrategyStatisVO vo=null;
//        //判断vo对象是否已经在redis存在
//        for (StrategyDetail detail : list) {  //foreach：iter
//            if(strategyStatisRedisService.isStrategyStatisVOExsit(detail.getId())){
//                continue;
//            }
//            vo=new StrategyStatisVO();
//            BeanUtil.copyProperties(vo,detail);
//            vo.setDestId(detail.getDest().getId());
//            vo.setDestName(detail.getDest().getName());
//            vo.setStrategyId(detail.getId());
//            strategyStatisRedisService.setStrategyStatisVO(vo);
//        }

        System.out.println("---------------初始化开始-----------------------");
        List<StrategyDetail> list = strategyDetailService.list();
        StrategyStatisVO vo = null;
        System.out.println("---------------初始化结束-----------------------");

        System.out.println("---------------Hot初始化开始-----------------------");
        for (StrategyDetail detail : list){
            String value = RedisKeys.STRATEGY_STATIS_VO.join(detail.getId().toString());
            //参数1: zset的key   参数2:zset 的value
            if(strategyStatisRedisService.isZsetHasValue(RedisKeys.STRATEGY_STATIS_HOT_SORT.getPrefix(), value)){
                continue;
            }
            //取浏览数+评论数
            int score = detail.getViewnum() + detail.getReplynum();
            strategyStatisRedisService.addHotScores(value, score);
        }
        System.out.println("---------------Hot初始化结束-----------------------");

        System.out.println("---------------国内外初始化开始-----------------------");
        for (StrategyDetail strategyDetail : list) {
            //为了防止重复启动mgrsite项目,多次执行初始化话覆盖redis中排序数据, 需要进行排除
            //如果zset数据已经存在,直接跳过
            String value = RedisKeys.STRATEGY_STATIS_VO.join(strategyDetail.getId().toString());
            //参数1: zset的key   参数2:zset 的value
            if(strategyStatisRedisService.isZsetHasValue(RedisKeys.STRATEGY_STATIS_COMMEND_SORT.getPrefix(), value)){
                continue;
            }
            //点赞数+收藏数
            int score = strategyDetail.getThumbsupnum() + strategyDetail.getFavornum();
            strategyStatisRedisService.addCommendScores(value, score);
        }
        System.out.println("---------------国内外初始化结束-----------------------");
    }
}
